<?php
$username = $_GET["username"];
$pwd = $_GET["pwd"];

if (!$username || !$pwd) {
    die('{"code":"101","msg":"参数不足"}');
}

require("./connect.php");

$sql1 = "SELECT `username` FROM `user`";
$res1 = mysqli_query($con, $sql1);
$arr = mysqli_fetch_all($res1, MYSQLI_ASSOC);

for ($i = 0; $i < count($arr); $i++) {
    if ($arr[$i]['username'] === $username) {
        die('{ "code":"102","msg":"用户已存在"}');
    }
}
$select_sql = "INSERT INTO `user` VALUES (null,'$username','$pwd',null)";
$res = mysqli_query($con, $select_sql);

if (!$res) {
    die('{"code":"202","msg":"数据库错误","detail":"' . mysqli_error($con) . '"}');
}
echo '{"code":"0","msg":"注册成功"}';

// echo "注册成功";
mysqli_close($con);
